/*
 * Copyright (c) 2006, 2021, webrx.cn All rights reserved.
 *
 */
package cn;


import cn.webrx.entity.Student;
import cn.webrx.entity.Teacher;
import cn.webrx.entity.User;
import cn.webrx.mapper.StudentMapper;
import cn.webrx.mapper.TeacherMapper;
import cn.webrx.mapper.UserMapper;
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.Environment;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.transaction.TransactionFactory;
import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.*;
import java.sql.Blob;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * <p>Project: mybatis2021 - Demo
 * <p>Powered by webrx On 2021-07-12 16:09:44
 * <p>Created by IntelliJ IDEA
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 15
 */
@Slf4j
public class Demo {
    protected StudentMapper sm;
    protected UserMapper um;
    protected SqlSession ss;

    @After
    public void close() {
        ss.commit();
    }

    @Before
    public void init() throws IOException {
        //建立数据源，
        DruidDataSource ds = new DruidDataSource();
        Properties prop = new Properties();
        prop.load(Resources.getResourceAsReader("db.properties"));
        ds.setConnectProperties(prop);

        TransactionFactory tx = new JdbcTransactionFactory();
        Environment env = new Environment("development", tx, ds);
        Configuration cfg = new Configuration(env);

        cfg.addMappers("cn.webrx.mapper");

        SqlSessionFactory sf = new SqlSessionFactoryBuilder().build(cfg);
        ss = sf.openSession();

        sm = ss.getMapper(StudentMapper.class);
        um = ss.getMapper(UserMapper.class);
    }

    @Test
    public void t2() throws SQLException {
        TeacherMapper tm = ss.getMapper(TeacherMapper.class);
        Teacher tc = new Teacher();
        tc.setName("周老师");
        byte[] buf = null;
        try {
            FileInputStream fis = new FileInputStream("d:/download/dog/pic.jpg");
            buf = fis.readAllBytes(); //readAllBytes() java 9 新增的方法
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        tc.setPic(buf);
        tc.setRemark("周1234234234");
        //int row = tm.save(tc);
        //System.out.println(row);


       // Teacher t = tm.pic("1d46c58e-e3bc-11eb-9d45-00ff9ba3cf09");
        //System.out.println(t.getPic());
        Map<String,byte[]> map = tm.pic("1d46c58e-e3bc-11eb-9d45-00ff9ba3cf09");

        try {

            FileOutputStream fos = new FileOutputStream("c:/1d46c58e-e3bc-11eb-9d45-00ff9ba3cf09.jpg");
            fos.write(map.get("pic")); //
            fos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void t1() {
        User u = new User();
        u.setName("李四");
        u.setBirth(LocalDate.parse("1982-10-18"));
        u.setRegtime(new Date());
        u.setLogintime(LocalTime.now());
        u.setUpdatetime(LocalDateTime.now());
        //int row = um.save(u);
        //System.out.println(row);

        System.out.println(um.queryAll());
    }

    @Test
    public void show() {
        System.out.println(sm.dbs());
        System.out.println(sm.tbs());
        System.out.println(sm.tbs("hbcf"));
        System.out.println(sm.shows());
        System.out.println(sm.shows2());
        System.out.println(sm.count("t_user")); //只有一个值
        Map<String, Object> map = sm.mycount();
        System.out.println(map.get("c"));
        System.out.println(map.get("m"));
        System.out.println(map.get("a"));
        log.info(sm.queryBys(400));
        System.out.println(sm.showNames());


        //java.util.Date
        //java.time.LocalDate
        //java.time.LocalDateTime
        //java.sql.Timestamp
        //java.sql.Date
    }

    @Test
    public void update() {
        //sm.update(10);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("money", 600); //money = #{money}     money = money + #{money} money = money - #{money}
        map.put("name", "李%");
        map.clear();
        map.put("money", 15);
        map.put("inc", false);
        map.put("name", "李%");
        map.clear();
        map.put("id", 39);
        map.put("money", 100);
        map.put("inc", true);
        //sm.updateByNameMoney(map);

        //查询对象
        Student s = sm.queryById(32);
        if (s != null) {
            s.setName("李燕");
            s.setAge(55);
            int row = sm.updateByStudent(s);
            log.info(row + "");
        } else {
            log.info("无法修改数据");
        }

        log.info((sm.queryAll().toString()));

    }

    @Test
    public void select() {
        //int row = sm.deleteById(47);
        //int row = sm.deleteByNameAge("赵丰", 32);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 44);
        map.put("age", 55);
        map.clear();
        map.put("id", 35);
        map.put("name", "李丽");
        int row = sm.deleteBy(map);
        log.debug(row + "");
        log.info((sm.queryAll().toString()));

        //sm.cdb("userdb2");

    }


    @Test
    public void insert() {
        log.info("测试插入数据");
        //log.debug(sm.ver());
        //log.info(String.valueOf(sm.add("李四六", 21, "他家是郑州的.", 700d)));
        Student st = new Student();

        st.setName("李丽88");

        st.setMoney(1.5);
        log.info(String.valueOf(sm.save(st)));
        System.out.println(st.getId());

        //log.info(String.valueOf(sm.add("李四六", 21, "他家是郑州的.", 700d)));

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("name", "李强3");
        map.put("age", 33);
        map.put("info", "l3");
        map.put("money", 33f);
        sm.insert(map);
        System.out.println(map.get("id"));

        map.clear();
        map.put("money", 10f);
        map.put("age", 55);
        map.put("name", "jack");


        sm.append(map);
        System.out.println(map.get("id"));


    }
}
